package com.zzk.lanouhn.myapplication1.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.InvestPinglunAuthorFensi;
import com.zzk.lanouhn.myapplication1.activity.InvestPinglunAuthorGuanzhu;
import com.zzk.lanouhn.myapplication1.activity.InvestPinglunAuthorPhoto;
import com.zzk.lanouhn.myapplication1.modles.InvestPingLunAuthorModle;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by lanouhn on 16/9/18.
 */
public class InvestPingLunAuthorAdapter extends RecyclerView.Adapter<InvestPingLunAuthorAdapter.MyViewholder> {

    private Context context;
    private InvestPingLunAuthorModle beanList;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    public InvestPingLunAuthorAdapter(Context context, InvestPingLunAuthorModle beanList) {
        this.context = context;
        this.beanList = beanList;
    }

    @Override
    public MyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_invest_pinglun_author, null);
        MyViewholder viewholder = new MyViewholder(view);
        return viewholder;
    }

    @Override
    public void onBindViewHolder(MyViewholder holder, int position) {

        if (beanList.getData().getUser_lol_info() == null) {
            holder.rl.setVisibility(View.GONE);

            holder.tv_zuozhe.setText(beanList.getData().getNickname());
            holder.tv_ID.setText("掌游宝ID：" + beanList.getData().getUserid());
            //判断是否有年龄
            if (beanList.getData().getSex() == null) {
                holder.tv_baomi.setVisibility(View.VISIBLE);
            } else {
                //判断男女
                if (beanList.getData().getSex().equals("1")) {
                    holder.iv_nan.setVisibility(View.VISIBLE);
                    holder.iv_nv.setVisibility(View.GONE);
                    holder.tv_baomi.setVisibility(View.GONE);
                } else {
                    holder.iv_nan.setVisibility(View.GONE);
                    holder.iv_nv.setVisibility(View.VISIBLE);
                    holder.tv_baomi.setVisibility(View.GONE);
                }
            }
            if (beanList.getData().getCity() == null) {
                holder.tv_weizhi.setText("未知");
            } else {
                holder.tv_weizhi.setText(beanList.getData().getCity());
            }

            holder.tv_age.setText(beanList.getData().getAge());
            //判断是否有签名
            if (beanList.getData().getSignature() == null) {
                holder.tv_qianming.setText("这家伙很懒，没写签名...");
            } else {
                holder.tv_qianming.setText(beanList.getData().getSignature());
            }
            holder.tv_pop.setText(beanList.getData().getPhotos());
            holder.tv_dongtai.setText(beanList.getData().getPublishes());
            holder.tv_guanzhu.setText(beanList.getData().getAttentions());
            holder.tv_fensi.setText(beanList.getData().getFans());

            Picasso.with(context).load(beanList.getData().getAvatar_small()).into(holder.civ_touxiang);

        } else {
            holder.rl.setVisibility(View.VISIBLE);

            sp = context.getSharedPreferences("photo", Context.MODE_PRIVATE);
            editor = sp.edit();

            holder.tv_zuozhe.setText(beanList.getData().getNickname());
            holder.tv_ID.setText("掌游宝ID：" + beanList.getData().getUserid());
            //判断是否有年龄
            if (beanList.getData().getSex() == null) {
                holder.tv_baomi.setVisibility(View.VISIBLE);
            } else {
                //判断男女
                if (beanList.getData().getSex().equals("1")) {
                    holder.iv_nan.setVisibility(View.VISIBLE);
                    holder.iv_nv.setVisibility(View.GONE);
                    holder.tv_baomi.setVisibility(View.GONE);
                } else {
                    holder.iv_nan.setVisibility(View.GONE);
                    holder.iv_nv.setVisibility(View.VISIBLE);
                    holder.tv_baomi.setVisibility(View.GONE);
                }
            }

            if (beanList.getData().getCity() == null) {
                holder.tv_weizhi.setText("未知");
            } else {
                holder.tv_weizhi.setText(beanList.getData().getCity());
            }

            holder.tv_age.setText(beanList.getData().getAge());
            //判断是否有签名
            if (beanList.getData().getSignature() == null) {
                holder.tv_qianming.setText("这家伙很懒，没写签名...");
            } else {
                holder.tv_qianming.setText(beanList.getData().getSignature());
            }
            holder.tv_pop.setText(beanList.getData().getPhotos());
            holder.tv_dongtai.setText(beanList.getData().getPublishes());
            holder.tv_guanzhu.setText(beanList.getData().getAttentions());
            holder.tv_fensi.setText(beanList.getData().getFans());

            Picasso.with(context).load(beanList.getData().getAvatar_small()).into(holder.civ_touxiang);

            Picasso.with(context).load(beanList.getData().getUser_lol_info().getIcon()).into(holder.iv_touxiang);
            holder.tv_nicheng.setText(beanList.getData().getUser_lol_info().getSummoner());
            holder.tv_qu.setText(beanList.getData().getUser_lol_info().getArea());
            holder.tv_zdl.setText(beanList.getData().getUser_lol_info().getZdl());

            final String Authorss = beanList.getData().getUser_lol_info().getUser_id();

            //跳转粉丝页面
            holder.ll_fensi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, InvestPinglunAuthorFensi.class);
                    intent.putExtra("fensiID", Authorss);
                    context.startActivity(intent);
                }
            });
            //跳转到关注页面
            holder.ll_guanzhu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, InvestPinglunAuthorGuanzhu.class);
                    intent.putExtra("guanzhuID", Authorss);
                    context.startActivity(intent);
                }
            });
            //跳转到相册页面
            holder.ll_pop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, InvestPinglunAuthorPhoto.class);
                    intent.putExtra("photo", Authorss);
                    editor.putString("photodatu", Authorss);
                    editor.commit();
                    context.startActivity(intent);
                }
            });


        }
        //返回
        holder.iv_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) context).finish();
            }
        });


    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public class MyViewholder extends RecyclerView.ViewHolder {

        private ImageView iv_fanhui, iv_touxiang, iv_nan, iv_nv;
        private CircleImageView civ_touxiang;
        private TextView tv_zuozhe, tv_ID, tv_age, tv_weizhi, tv_pop, tv_dongtai, tv_guanzhu, tv_fensi, tv_qianming, tv_baomi;

        private TextView tv_nicheng, tv_qu, tv_zdl;

        private RelativeLayout rl;

        private LinearLayout ll_pop, ll_dongtai, ll_guanzhu, ll_fensi;

        public MyViewholder(View itemView) {
            super(itemView);

            iv_fanhui = (ImageView) itemView.findViewById(R.id.pinglun_author_iv_fanhui);
            iv_touxiang = (ImageView) itemView.findViewById(R.id.pinglun_author_rl_touxiang);
            iv_nan = (ImageView) itemView.findViewById(R.id.pinglun_author_iv_Nan);
            iv_nv = (ImageView) itemView.findViewById(R.id.pinglun_author_iv_NV);

            civ_touxiang = (CircleImageView) itemView.findViewById(R.id.pinglun_author_iv_zuozhe_touxiang);

            tv_zuozhe = (TextView) itemView.findViewById(R.id.pinglun_author_tv_zuozhe);
            tv_ID = (TextView) itemView.findViewById(R.id.pinglun_author_tv_ID);
            tv_age = (TextView) itemView.findViewById(R.id.pinglun_author_tv_age);
            tv_weizhi = (TextView) itemView.findViewById(R.id.pinglun_author_tv_weizhi);
            tv_pop = (TextView) itemView.findViewById(R.id.pinglun_author_ll_xiangce);
            tv_dongtai = (TextView) itemView.findViewById(R.id.pinglun_author_ll_dongtai);
            tv_guanzhu = (TextView) itemView.findViewById(R.id.pinglun_author_ll_guanzhu);
            tv_fensi = (TextView) itemView.findViewById(R.id.pinglun_author_ll_fensi);
            tv_qianming = (TextView) itemView.findViewById(R.id.pinglun_author_tv_qianming);
            tv_baomi = (TextView) itemView.findViewById(R.id.pinglun_author_tv_baomi);

            tv_nicheng = (TextView) itemView.findViewById(R.id.pinglun_author_rl_nicheng);
            tv_qu = (TextView) itemView.findViewById(R.id.pinglun_author_rl_qu);
            tv_zdl = (TextView) itemView.findViewById(R.id.pinglun_author_rl_zdl);

            rl = (RelativeLayout) itemView.findViewById(R.id.pinglun_author_rl);

            //四个跳转按钮
            ll_pop = (LinearLayout) itemView.findViewById(R.id.pinglun_author_xiangce);
            ll_dongtai = (LinearLayout) itemView.findViewById(R.id.pinglun_author_dongtai);
            ll_guanzhu = (LinearLayout) itemView.findViewById(R.id.pinglun_author_guanzhu);
            ll_fensi = (LinearLayout) itemView.findViewById(R.id.pinglun_author_fensi);


        }
    }
}
